package com.sls.api.game.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.sls.api.game.pojo.GoodsTransactionInfo;
import com.sls.api.util.IpUtil;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;

/**
 * 
 * @description 测试用
 * @author Wangxuegang
 * @date 2012-12-13
 */
public class TestNotifyUrlServlet extends SuperServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(TestNotifyUrlServlet.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setAttribute("org.eclipse.jetty.server.Request.queryEncoding",
				"UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		resp.setStatus(HttpServletResponse.SC_OK);
		
//		String test = req.getParameter("parameter");
		
		ServletInputStream stream = req.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(stream));
		String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }
//        String result = sb.toString().trim();
		
		String clientIp = IpUtil.getIpAddr(req);
		log.info("============客户端真实Ip：" + clientIp);

		// 合作方订单编号
		String ptOrderNo = req.getParameter("ptOrderNo");
		// 手拉手订单编号
		String supOrderNo = req.getParameter("supOrderNo");
		// 订单处理结果
		String status = req.getParameter("status");
		// 手拉手充值成功时间
		String supOrderSuccessTime = req.getParameter("supOrderSuccessTime");
		// 失败原因
		String failedReason = req.getParameter("failedReason");

		log.info("订单处理的最终结果：" + "\n合作方订单编号:" + ptOrderNo + "\n手拉手订单编号:"
				+ supOrderNo + "\n订单处理结果:" + status + "\n手拉手手拉手充值成功时间："
				+ supOrderSuccessTime + "\n失败原因:" + failedReason);

		@SuppressWarnings("unused")
		Gson gson = CommonUtil.gson();
		@SuppressWarnings("unused")
		SocketDTO socketChangeVo = createMsg(ptOrderNo, supOrderNo, status,
				supOrderSuccessTime, failedReason, req);
		
		
		String value = "SUCCESS";
		if ("5".equals(ptOrderNo)) {
			 value = "SUCCESS";
		}
		
		StringBuilder strBui = new StringBuilder();
		strBui.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBui.append("<response>");
		strBui.append("<handleResult>");
		strBui.append(value);
		strBui.append("</handleResult>");
		strBui.append("</response>");
		
				resp.getWriter().write(strBui.toString().trim());
				resp.getWriter().flush();
				resp.getWriter().close();

	}

	private SocketDTO createMsg(String ptOrderNo, String supOrderNo,
			String status, String supOrderSuccessTime, String failedReason,
			HttpServletRequest req) {
		GoodsTransactionInfo info = new GoodsTransactionInfo();
		info.setPtOrderNo(ptOrderNo);
		info.setSupOrderNo(supOrderNo);
		info.setStatus(status);
		info.setSupOrderSuccessTime(supOrderSuccessTime);
		info.setFailedReason(failedReason);

		SocketDTO socketChangeVo = new SocketDTO();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020904");
		head.setIp(IpUtil.getIpAddr(req)); // ---------坚果网络IP
		head.setUserId("0"); // ---------坚果网络用户ID
		socketChangeVo.setHead(head); // 报文头部
		return super.send(head, info);

	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

}
